#include<iostream>
using namespace std;

typedef long long ll;


ll power(ll a, ll b, ll q)
{
	ll ret=0, sign=1;
	while(b)
	{
		if(b&1) ret=(ret+(a*sign))%q;
		sign=(sign*2)%q;
		b>>=1;
	}
	return ret;
}
int main()
{
	ll a, b, q;
	cin>>a>>b>>q;
	cout<<power(a, b, q);
	return 0;
}
